from rest_framework import serializers
from rest_framework.validators import UniqueTogetherValidator

from .models import FollowProblem, FollowUser, FavAnswer, AgreeOppositeAnwser, ProblemToTag
from commando.serializers import ProblemDetailSerializer, AnswerDetailSerializer
from settings import TAG_NUM
from .validators import ProblemToTagValidators, AgreeOppositeValidator


class FollowProblemDetailSerializer(serializers.ModelSerializer):
    problem = ProblemDetailSerializer()
    add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M')

    class Meta:
        model = FollowProblem
        fields = ('problem', 'add_time')


class FollowProblemSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())

    class Meta:
        model = FollowProblem
        fields = ('user', 'problem')  # 这个代表收藏之后的返回
        validators = [
            UniqueTogetherValidator(
                queryset=FollowProblem.objects.all(),
                fields=('user', 'problem'),  # 这里的fields是做联合索引的fields
                message="已关注该问题"
            )]


class FavAnswerDetailSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    answer = AnswerDetailSerializer()

    class Meta:
        model = FavAnswer
        fields = ('user', 'answer')


class FavAnswerSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())

    class Meta:
        model = FavAnswer
        fields = ('user', 'answer')  # 这个代表收藏之后的返回
        validators = [
            UniqueTogetherValidator(
                queryset=FavAnswer.objects.all(),
                fields=('user', 'answer'),  # 这里的fields是做联合索引的fields
                message="已收藏该回答"
            )]


class AgreeOppositeAnswerSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())

    class Meta:
        model = AgreeOppositeAnwser
        fields = ('answer', 'user', 'agree', 'opposite')

        validators = [
            AgreeOppositeValidator()
        ]


class ProblemToTagSerializer(serializers.ModelSerializer):
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())

    class Meta:
        model = ProblemToTag
        fields = ('user', 'id', 'problem', 'tag')
        validators = [
            UniqueTogetherValidator(
                queryset=ProblemToTag.objects.all(),
                fields=('tag', 'problem'),
                message='已为该问题添加该标签',
            ),
            ProblemToTagValidators(tag_num=TAG_NUM, err_msg='一个问题最多能添加{tag_name}个标签'.format(tag_name=TAG_NUM)),
        ]


class FollowUserSerializer(serializers.ModelSerializer):
    following_user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    add_time = serializers.DateTimeField(read_only=True, format='%Y-%m-%d %H:%M')

    class Meta:
        model = FollowUser
        fields = '__all__'
        validators = [
            UniqueTogetherValidator(
                queryset=FollowUser.objects.all(),
                fields=('following_user','followed_user'),
                message='你已关注该用户，不能重复关注'
            )
        ]
